// pages/carts/carts.js
import myrequest from "../../utils/myrequest.js"
Page({
    
    /**
     * 页面的初始数据
     */
    data: {
        url: "http://localhost:2100",
        hots: [],
        carts:[],
        show:false,
        num:null,
        count:1,
        checked:false,
        command:[],
        total:0,
        edit:"编辑",
        btnzi:"结算",
        newarrs:null,
        id:null,
        all:false
    },
    
    /**
     * 生命周期函数--监听页面加载
     */
    // methods
    gethots(){  //获取热卖推荐数据
        wx.request({
          url: this.data.url + "/hots",
          success: (res)=>{
            console.log(res.data.data)
            this.setData({
              hots: res.data.data
            })
          }
        })
      },
      onitem(e){ //热卖推荐-商品点击
        let hot = e.currentTarget.dataset.hot
        console.log(hot)
        console.log(hot)
        let jilu = wx.getStorageSync('jilu') || []
        
        this.setData({jilu})
        this.data.jilu.push(hot)
        console.log(this.data.jilu)
        wx.setStorageSync('jilu', this.data.jilu)
        wx.navigateTo({
            url: "/pages/indetail/indetail?id=" + hot.id,
        })
        console.log(e)
      },
    onall(){
        let that = this
        this.setData({
        all:!this.data.all
        })
        if(that.data.all){
        this.setData({
        carts:this.data.carts.map(item=>{item.checked = true;return item})
        })
        }else{
        this.setData({
        carts:this.data.carts.map(item=>{item.checked = false;return item})
        })
        }
        wx.setStorageSync('carts',this.data.carts)
        let newarrs = this.data.carts.filter((item,index)=>{
            return item.checked == true
            })
            
        let total = newarrs.reduce((num,item)=>{
            return num + item.price*item.count
        },0)
        this.setData({
            total
        })

        },

        onchange2(event){
            console.log(event)
            let that = this
            let id = event.currentTarget.dataset.id;
            console.log(id)
            let truel = 0
            this.data.carts.forEach(function (item,index){
            if (item.id == id) {
            item.checked = !item.checked
            }
            if (item.checked) {
            truel++
            }
            })
            if (truel*2 == this.data.carts.length) {
            that.setData({
            all:true
            })
            }else{
            that.setData({
            all: false
            })
            }
            
            this.setData({
            carts:this.data.carts
           })
           console.log(this.data.carts)
            wx.setStorageSync('datas',this.data.carts)
            let newarrs = this.data.carts.filter((item,index)=>{                
                return item.checked == true
                })
               
                
            let total = newarrs.reduce((num,item)=>{
                return num + item.price*item.count
            },0)
            this.setData({
                total,
                newarrs
            })
            console.log(this.data.newarrs)
        },


    tdfn(){
        let that = this
        if(this.data.btnzi=="结算"){
            console.log("====")
            that.totalfn()
        }else if(this.data.btnzi=="删除"){
            that.delefn()
        }

    },

    totalfn(){
        wx.navigateTo({
            url: '/pages/order/order?orders='+JSON.stringify(this.data.newarrs)
          })
    },

    delefn(){
        let index = this.data.id
        let carts = wx.getStorageSync('carts')
        carts.splice(index,1)
        wx.removeStorageSync("carts",index)
        wx.setStorageSync('carts',carts)
        console.log(carts)
        this.getcarts()
    },

   
    clickfn(){
        this.setData({
            let:!this.data.let
        })
    },
    editfn(event){
        console.log(event)
        if(this.data.edit=="编辑"){
            this.setData({
                edit:"完成",
                btnzi:"删除"

            })
        }else{
            this.setData({
                edit:"编辑",
                btnzi:"结算"
            })

        }
        
    },
   

    checkfn(event){
        this.setData({
            checked:!this.data.checked
        })
        this.data.carts.forEach((item)=>{
            item.checked = true
        })
        // console.log(this.data.carts)
    },


    onChange(event){
        let that = this
        let id = event.currentTarget.dataset.id
        let newarrs = this.data.newarrs
        console.log(event)
        let count = event.detail
        this.setData({
            count
        })
        this.data.carts.forEach(function (item,index){
            if(id == item.id){
                item.count = that.data.count
                return item
            }     
        })
        this.setData({
            carts:this.data.carts
        })
        // console.log(this.data.carts)  
        let total = newarrs.reduce((num,item)=>{
            return num + item.price*item.count
        },0)
        this.setData({
            total
        }) 
       
    },

    async getcommand(){
        let {data} = await myrequest({url:"/cartsStorges"})
        this.setData({
            command:data.data
        })
    },

    getcarts(){   
        let carts = wx.getStorageSync('carts')
        if(carts.length!==0){
            carts.forEach((item)=>{
                item.count = 1
                console.log(item.count)
                item.checked = false
            })
        }else{
            carts=[]
        }
        
        this.setData({
            carts
        })
        
        console.log(this.data.carts)
    },

     
    onClose() {
        this.setData({ show: false });
      },


    onLoad: function (options) {
        // this.getcommand()
       this.gethots()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getcarts()
        let num = this.data.carts.length
        this.setData({
            num
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})